package com.lx.request;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo2")
public class RequestDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据参数名称获取参数值
        String username = req.getParameter("username");
        //根据参数名称获取参数的数组
        String[] parameterValues = req.getParameterValues("username");
        for (String parameterValue : parameterValues){
            System.out.println(parameterValue);
        }

    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理中文乱码(tomcate8.0以上版本 get方式不会出现乱码)
        req.setCharacterEncoding("utf-8");
        //存储数据到request作用域中(移除Attribute  req.removeAttribute();)
        req.setAttribute("a","hello");
        //获取servletContext
        ServletContext servletContext = req.getServletContext();
        //请求转发
        req.getRequestDispatcher("/hello").forward(req,resp);
    }
}
